package com.qiyuesuo.client.controller;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiyuesuo.client.PO.FileInfo;
import com.qiyuesuo.client.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author s77
 * @create 2020-4月-03 9:57
 */
@Controller
@RequestMapping("/file")
public class FileController {

    private final static Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    @Qualifier("fileService")
    private FileService fileService;


    @RequestMapping("info")
    @ResponseBody
    public FileInfo getFileInfo(String name) {
        String REQUEST_PATH = "http://localhost:8081/info?name=" + name;
        String METHOD = "GET";
        HttpURLConnection httpConn = fileService.gethttpConn(REQUEST_PATH, METHOD);
        if (httpConn != null) {
            String fileInfoJson = fileService.info(httpConn);
            //转成对象，屏蔽存放位置，数字信封信息
            ObjectMapper om = new ObjectMapper();
            FileInfo fileInfo = null;
            try {
                fileInfo = om.readValue(fileInfoJson, FileInfo.class);
                System.out.println(fileInfo);
                fileInfo.setDirectoryAddress(null);
                fileInfo.setDigitalEnvelope(null);
            } catch (IOException e) {
                logger.error("json 转 fileInfo 对象失败：" + fileInfoJson, e);
            }
            httpConn.disconnect();
            return fileInfo;
        }
        httpConn.disconnect();
        return null;


    }

    @RequestMapping("newinfo")
    @ResponseBody
    public List<FileInfo> getNewFileInfo(int num) {
        String REQUEST_PATH = "http://localhost:8081/newinfo?num=" + num;
        String METHOD = "GET";

        HttpURLConnection httpConn = fileService.gethttpConn(REQUEST_PATH, METHOD);
        if (httpConn != null) {
            String listInfoJson = fileService.info(httpConn);
            //转成对象，屏蔽存放位置，数字信封信息
            ObjectMapper om = new ObjectMapper();
            List<FileInfo> list = null;
            try {
                ObjectMapper mapper = new ObjectMapper();
                JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, FileInfo.class);
                list = (List<FileInfo>) mapper.readValue(listInfoJson, javaType);
                for (FileInfo fileInfo : list) {
                    fileInfo.setDirectoryAddress(null);
                    fileInfo.setDigitalEnvelope(null);
                }
            } catch (IOException e) {
                logger.error("json 转 fileInfo 对象失败：" + listInfoJson, e);
            }
            httpConn.disconnect();
            return list;
        }
        httpConn.disconnect();
        return null;
    }

}
